package com.bdqn.crm;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.subject.Subject;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;


@SpringBootTest
public class ShiroTester {

    @Test
    public void testShiro(){
        //1、创建 Realm(安全数据源)
        //通过 shiro.ini 配置文件创建 Realm
        IniRealm realm = new IniRealm("classpath:shiro.ini");
        //配置SecurityManager
        DefaultSecurityManager securityManager = new DefaultSecurityManager();

        //注入创建的 Realm(安全数据源)
        securityManager.setRealm(realm);
        SecurityUtils.setSecurityManager(securityManager);
        //3、操作 Subject，进行认证
        Subject subject = SecurityUtils.getSubject();

        //封装一个令牌
        UsernamePasswordToken token = new UsernamePasswordToken("czkt","111111");
        try{
            subject.login(token);
        }catch (AuthenticationException e){
            System.out.println("认证异常");
            e.printStackTrace();
        }
        System.out.println("是否认证通过:"+subject.isAuthenticated());
        System.out.println("是否为管理员角色："+subject.hasRole("客户经理"));
        System.out.println("是否能操作用户查看功能:"+subject.isPermitted("user:view"));
    }

    @Test
    public void testMd5Hash(){
         String password = "admin";
         String salt = "czkt";
         Md5Hash md5Hash = new Md5Hash(password, salt);
         System.out.println(md5Hash.toString());
         //输出：36aa3e095a86925b7658b5d00557fa
    }
}
